<template>
	<view>
		<view v-for="item in goodsList" :key="item.goods_id" @click="haodgoto(item.goods_id)">
			<my-goods :goolistobj="item"></my-goods>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数的对象
				queryObj:{
					//关键字
					query:'',
					//商品分类id
					cid:'',
					//页码，
					pagenum:1,
					//展示条数据
					pagesize:10
				},
				//商品列表的数据
				goodsList:[],
				//总数量，用来实现分页
				total:0,
				//节流
				isloaddlg:false
			};
		},
		onLoad(options){
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.id || ''
			//获取商品列表数据
			this.getGoodsList()
		},
		methods:{
			//点击商品跳到详情
			haodgoto(id){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?id=' + id
				})
			},
			//获取商品数据
			async getGoodsList(){
				this.isloaddlg = true
				const {data:res} = await uni.$http.get('/goods/search',this.queryObj)
				if(res.meta.status == 200){
					if(this.queryObj.pagenum !=1){
						res.message.goods.map(item=>{
							if(item.goods_small_logo == ''){
								item.goods_small_logo = 'https://img1.baidu.com/it/u=101707035,4025270870&fm=253&fmt=auto&app=138&f=JPEG?w=320&h=240'
							}
						})
						this.goodsList = [...this.goodsList,...res.message.goods]
						this.total = res.message.total
						this.isloaddlg = false
					}else{
						res.message.goods.map(item=>{
							if(item.goods_small_logo == ''){
								item.goods_small_logo = 'https://img1.baidu.com/it/u=101707035,4025270870&fm=253&fmt=auto&app=138&f=JPEG?w=320&h=240'
							}
						})
						this.goodsList = res.message.goods
						this.total = res.message.total
						this.isloaddlg = false
						//关闭下拉加载
						uni.stopPullDownRefresh()
					}
				}else{
					uni.$showMsg()
					this.isloaddlg = false
				}
			}
		},
		//下拉加载
		onPullDownRefresh(){
			this.queryObj.pagenum = 1
			this.total = 0
			this.isloaddlg = false
			this.goodsList = []
			this.getGoodsList()
		},
		//上拉加载回调
		onReachBottom(){
			if(this.goodsList.length == this.total){
				uni.$showMsg('已经到底啦~')
				return
			}
			if(this.isloaddlg) return
			this.queryObj.pagenum ++
			this.getGoodsList()
		}
	}
</script>

<style lang="scss" scoped>
</style>
